using System.Collections.Generic;
using UnityEngine;

namespace Yoozoo.Gameplay.RTS
{
    public class SelfSelectControl : MonoBehaviour
    {
        public GameObject range1;
        public GameObject range2;
        public GameObject range3;
        public GameObject leida;
        public GameObject arrowTry;
        public GameObject arrowFull;

        private float leidaScale;
        private List<GameObject> tryList = new List<GameObject>();
        private List<GameObject> fullList = new List<GameObject>();
        private float scale1;
        private float scale2;
        private float scale3;

        private void Awake()
        {
            scale1 = range1.transform.localScale.x * (BattleUtils.Ranges[0] - BattleUtils.ArmyScale);
            scale2 = range2.transform.localScale.x * (BattleUtils.Ranges[1] - BattleUtils.ArmyScale);
            scale3 = range3.transform.localScale.x * (BattleUtils.Ranges[2] - BattleUtils.ArmyScale);
        }

        public void Init()
        {
            arrowTry.SetActive(false);
            if (tryList.Count == 0)
            {
                tryList.Add(arrowTry);
                for (int i = 0; i < 3; i++)
                {
                    tryList.Add(GameObject.Instantiate(arrowTry,transform,true));
                }
            }

            arrowFull.SetActive(false);
            if (fullList.Count == 0)
            {
                fullList.Add(arrowFull);
                for (int i = 0; i < 3; i++)
                {
                    fullList.Add(GameObject.Instantiate(arrowFull,transform,true));
                }
            }
            range1.transform.localScale = new Vector3(scale1,scale1,scale1);
            range2.transform.localScale = new Vector3(scale2,scale2,scale2);
            range3.transform.localScale = new Vector3(scale3,scale3,scale3);
        }

        public void SetForward(Vector3 forward)
        {
            /*Vector3 originForward = new Vector3(0, 0, 1);
            float angle = 0;
            if (forward.x > 0)
            {
                angle = (360 - Vector3.Angle(forward,originForward));
            }
            else
            {
                angle = Vector3.Angle(forward,originForward);
            }
            Vector3 originPosition = new Vector3(0,0,leidaScale * 0.9f);
            for (int i = 0; i < 4; i++)
            {
                Vector3 position = BattleUtils.GetPositionByAngleAndOriginPosition(originPosition, angle);
                tryList[i].transform.localPosition = position;
                fullList[i].transform.localPosition = position;
                if (position != Vector3.zero)
                {
                    tryList[i].transform.forward = -position;
                    fullList[i].transform.forward = -position;
                }
                angle += 90;
            }*/
        }
        
        public void Refresh(List<bool> rangeList,bool isTryAttack)
        {
            transform.localPosition = Vector3.zero;
            int range = 3;
            for (int i = 0; i < rangeList.Count; i++)
            {
                if (rangeList[i] && range > i + 1)
                {
                    range = i + 1;
                }
            }
            range1.SetActive(range == 1);
            range2.SetActive(range == 2);
            range3.SetActive(range == 3);
            
            leida.SetActive(false);
            /*Vector3 originPosition = new Vector3(0,0,leidaScale * 0.9f);

            float angle = 0;
            for (int i = 0; i < 4; i++)
            {
                Vector3 position = BattleUtils.GetPositionByAngleAndOriginPosition(originPosition, angle);
                GameObject arrow = tryList[i];
                tryList[i].SetActive(isTryAttack && range > 0);
                fullList[i].SetActive(!isTryAttack && range > 0);
                if (!isTryAttack)
                {
                    arrow = fullList[i];
                }
                arrow.transform.localPosition = position;
                if (position != Vector3.zero)
                {
                    arrow.transform.forward = -position;
                }
                angle += 90;
            }*/
            
        }
    }
}